package com.eedi.framework.social.controller.sys;

import com.eedi.framework.common.enums.UserTypeEnum;
import com.eedi.framework.common.pojo.CommonResult;
import com.eedi.framework.common.pojo.PageResult;
import com.eedi.framework.common.util.object.BeanUtils;
import com.eedi.framework.social.controller.sys.vo.user.SysSocialUserBindReq;
import com.eedi.framework.social.controller.sys.vo.user.SysSocialUserPageReq;
import com.eedi.framework.social.controller.sys.vo.user.SysSocialUserResp;
import com.eedi.framework.social.controller.sys.vo.user.SysSocialUserUnbindReq;
import com.eedi.framework.social.dal.dataobject.SysSocialUserDO;
import com.eedi.framework.social.convert.SysSocialUserConvert;
import com.eedi.framework.social.service.SysSocialUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import static com.eedi.framework.common.pojo.CommonResult.success;
import static com.eedi.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "系统界面 - 社交用户")
@RestController
@RequestMapping("/social-user")
@Validated
public class SysSocialUserController {

    @Resource
    private SysSocialUserService sysSocialUserService;

    @PostMapping("/bind")
    @Operation(summary = "社交绑定，使用 code 授权码")
    public CommonResult<Boolean> socialBind(@RequestBody @Valid SysSocialUserBindReq reqVO) {
        sysSocialUserService.bindSocialUser(SysSocialUserConvert.INSTANCE.convert(
                getLoginUserId(), UserTypeEnum.MEMBER_SYS, reqVO));
        return CommonResult.success(true);
    }

    @DeleteMapping("/unbind")
    @Operation(summary = "取消社交绑定")
    public CommonResult<Boolean> socialUnbind(@RequestBody SysSocialUserUnbindReq reqVO) {
        sysSocialUserService.unbindSocialUser(getLoginUserId(), UserTypeEnum.MEMBER_SYS, reqVO.getSysSocialType(), reqVO.getOpenid());
        return CommonResult.success(true);
    }

    // ==================== 社交用户 CRUD ====================

    @GetMapping("/get")
    @Operation(summary = "获得社交用户")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('sys:social-user:query')")
    public CommonResult<SysSocialUserResp> getSocialUser(@RequestParam("socialUserId") String socialUserId) {
        SysSocialUserDO socialUser = sysSocialUserService.getSocialUser(socialUserId);
        return success(BeanUtils.toBean(socialUser, SysSocialUserResp.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得社交用户分页")
    @PreAuthorize("@ss.hasPermission('sys:social-user:query')")
    public CommonResult<PageResult<SysSocialUserResp>> getSocialUserPage(@Valid SysSocialUserPageReq pageVO) {
        PageResult<SysSocialUserDO> pageResult = sysSocialUserService.getSocialUserPage(pageVO);
        return success(BeanUtils.toBean(pageResult, SysSocialUserResp.class));
    }

}
